//bange_aux.cpp
//Copyright (C) 2010-2011 Oscar (.teri) Triano

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <bange_aux.hpp>

bool bange::aux::ComposeColor(int index, sf::Color &color, lua_State *vm){
	//index is the table
    if (!lua_istable(vm, index)){
        return false;}
    lua_pushnumber(vm, 1);
    lua_gettable(vm, index);
    if (lua_isnumber(vm, -1)){
        color.r = static_cast<sf::Uint8>(lua_tonumber(vm, -1));}
    lua_pop(vm, 1);
    lua_pushnumber(vm, 2);
    lua_gettable(vm, index);
    if (lua_isnumber(vm, -1)){
        color.g = static_cast<sf::Uint8>(lua_tonumber(vm, -1));}
    lua_pop(vm, 1);
    lua_pushnumber(vm, 3);
    lua_gettable(vm, index);
    if (lua_isnumber(vm, -1)){
        color.b = static_cast<sf::Uint8>(lua_tonumber(vm, -1));}
    lua_pop(vm, 1);
    lua_pushnumber(vm, 4);
    lua_gettable(vm, index);
    if (lua_isnumber(vm, -1)){
        color.a = static_cast<sf::Uint8>(lua_tonumber(vm, -1));}
    lua_pop(vm, 1);
    return true;
}

void bange::aux::ComposeTupleFromColor(sf::Color &color, lua_State *vm){
    //Leave a value in the top of lua stack.
    lua_createtable(vm, 4, 0);
    lua_pushnumber(vm, color.r);
    lua_rawseti(vm, -2, 1);
    lua_pushnumber(vm, color.g);
    lua_rawseti(vm, -2, 2);
    lua_pushnumber(vm, color.b);
    lua_rawseti(vm, -2, 3);
    lua_pushnumber(vm, color.a);
    lua_rawseti(vm, -2, 4);
}

/*
int bangeaux_ComposeVector(int index, cpVect *vect, lua_State *vm){
    if(!lua_istable(vm, index)){
        return 0;}
    lua_rawgeti(vm, index, 1);
    vect->x = lua_tonumber(vm, -1);
    lua_pop(vm, 1);
    lua_rawgeti(vm, index, 2);
    vect->y = lua_tonumber(vm, -1);
    lua_pop(vm, 1);
    return 1;
}

void bangeaux_ComposeTupleFromVector(cpVect vect, lua_State *vm){
    lua_createtable(vm, 2, 0);
    lua_pushnumber(vm, vect.x);
    lua_setfield(vm, -2, "x");
    lua_pushnumber(vm, vect.y);
    lua_setfield(vm, -2, "y");
}

int bangeaux_ObjectInsideTable(int table, void *object, lua_State *vm){
    void *currentobject = NULL;
    lua_rawgeti(vm, LUA_REGISTRYINDEX, table);
    lua_pushnil(vm);
    while (lua_next(vm, -2)){
        currentobject = lua_touserdata(vm, -1);
        if (currentobject == object){
            lua_pop(vm, 3);//value, key and table
            return 1;}
        //next
        lua_pop(vm, 1);
    }
    lua_pop(vm, 1);
    return 0;
}

void bangeaux_ComposeInfoWithArbiter(cpArbiter *arbiter, lua_State *vm){
    lua_createtable(vm, 0, 2);
    lua_pushboolean(vm, cpArbiterIsFirstContact(arbiter));
    lua_setfield(vm, -2, "FirstContact");
    bangeaux_ComposeTupleFromVector(cpArbiterGetNormal(arbiter, 0), vm);
    lua_setfield(vm, -2, "Normal");
    bangeaux_ComposeTupleFromVector(cpArbiterGetPoint(arbiter, 0), vm);
    lua_setfield(vm, -2, "Point");
    //And end
}

void bangeaux_ComposeIaPWithArbiter(cpArbiter *arbiter, lua_State *vm){
    lua_createtable(vm, 0, 7);
    lua_pushboolean(vm, cpArbiterIsFirstContact(arbiter));
    lua_setfield(vm, -2, "FirstContact");
    bangeaux_ComposeTupleFromVector(cpArbiterGetNormal(arbiter, 0), vm);
    lua_setfield(vm, -2, "Normal");
    bangeaux_ComposeTupleFromVector(cpArbiterGetPoint(arbiter, 0), vm);
    lua_setfield(vm, -2, "Point");
    bangeaux_ComposeTupleFromVector(cpArbiterTotalImpulse(arbiter), vm);
    lua_setfield(vm, -2, "Impulse");
    bangeaux_ComposeTupleFromVector(cpArbiterTotalImpulseWithFriction(arbiter), vm);
    lua_setfield(vm, -2, "ImpulseWithFriction");
    //And end
}
*/